package ui.panels;

import entities.Shift;
import java.awt.Font;
import java.util.Calendar;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import manager.ManagerDriver;
import model.EditShiftTableModel;
import ui.windows.AddShiftDialog;
import ui.windows.EditTimeSlotDialog;
import ui.windows.RemedySchedulerFrame;

public class TimeSlotManagementPanel extends javax.swing.JPanel {

    /** Creates new form TimeSlotManagementPanel */
    public TimeSlotManagementPanel(RemedySchedulerFrame mainFrame) {
        initComponents();
        this.mainFarme = mainFrame;
        includeModel();
    }

    private void includeModel() {
        driver = (ManagerDriver) mainFarme.getDriver();
        model = new EditShiftTableModel(mainFarme.getGenericDriver().showShift());
        shiftTable.setModel(model);
        shiftTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        shiftTable.setFillsViewportHeight(true);
        shiftTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        shiftTable.getTableHeader().setFont(new Font(null, Font.BOLD, 11));
        shiftTable.getColumnModel().getColumn(0).setPreferredWidth(40);
        shiftTable.getColumnModel().getColumn(1).setPreferredWidth(140);
        shiftTable.getColumnModel().getColumn(2).setPreferredWidth(75);
        shiftTable.getColumnModel().getColumn(3).setPreferredWidth(75);
    }

    public boolean createShift(Shift shift) {
        boolean returnVal = driver.insertShift(shift);
        if (returnVal) {
            shift.setModifiedDate(Calendar.getInstance().getTime());
            model.addElement(shift);
        }
        return returnVal;
    }

    public boolean updateShift(Shift shift) {
        boolean returnVal = driver.updateShift(shift);
        if (returnVal) {
            selectedShift.setStartTime(shift.getStartTime());
            selectedShift.setEndTime(shift.getEndTime());
            selectedShift.setModifiedDate(Calendar.getInstance().getTime());
            model.update(selectedShift);
        }
        return returnVal;
    }

    public boolean deleteShift(Shift shift) {
        boolean returnVal = driver.deleteShift(shift);
        if (returnVal) {
            model.remove(shift);
        }
        return returnVal;
    }

    public boolean containtID(int id) {
        return model.containtID(id);
    }

    public boolean containtName(String name) {
        return model.containtName(name);
    }

    private void switchAction() {
        int choise = cbActions.getSelectedIndex();
        cbActions.setSelectedIndex(0);
        switch (choise) {
            case 0:
                break;
            case 1:
                new Thread() {

                    @Override
                    public void run() {
                        showEditShift();
                    }
                }.start();
                break;
            case 2:
                new Thread() {

                    @Override
                    public void run() {
                        deleteAction();
                    }
                }.start();
                break;
        }
    }

    private void showCreateShift() {
        AddShiftDialog dialog = new AddShiftDialog(mainFarme, this);
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
    }

    private void showEditShift() {
        if (selectedShift != null) {
            final EditTimeSlotDialog dialog = new EditTimeSlotDialog(mainFarme, this);
            dialog.setSelectedRecord(selectedShift);
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
        } else {
            JOptionPane.showMessageDialog(mainFarme, "You migh select one record !");
        }
    }

    private void deleteAction() {
        if (selectedShift != null) {
            int choise = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this shift !", "Delete", JOptionPane.YES_NO_OPTION);
            if (choise == JOptionPane.YES_OPTION) {
                boolean returnVal = deleteShift(selectedShift);
                if (returnVal) {
                    JOptionPane.showMessageDialog(this, "Deleted");
                } else {
                    JOptionPane.showMessageDialog(this, "Cannot delete !", "Error", JOptionPane.WARNING_MESSAGE);
                }
            }
        } else {
            JOptionPane.showMessageDialog(mainFarme, "You migh select one record !");
        }
    }

    private void setSelectedShift() {
        int index = shiftTable.getSelectedRow();
        if (index != -1) {
            selectedShift = model.getAt(index);
        } else {
            selectedShift = null;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        viewShift = new javax.swing.JScrollPane();
        shiftTable = new javax.swing.JTable();
        bAdd = new javax.swing.JButton();
        cbActions = new javax.swing.JComboBox();

        setBackground(new java.awt.Color(255, 255, 255));

        viewShift.setBackground(new java.awt.Color(255, 255, 255));
        viewShift.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Shift list", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Agency FB", 1, 14))); // NOI18N

        shiftTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        shiftTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                shiftTableMouseClicked(evt);
            }
        });
        viewShift.setViewportView(shiftTable);

        bAdd.setText("Add");
        bAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAddActionPerformed(evt);
            }
        });

        cbActions.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Actions", "Edit", "Delete" }));
        cbActions.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbActionsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cbActions, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(viewShift, javax.swing.GroupLayout.PREFERRED_SIZE, 346, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(157, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(viewShift, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bAdd)
                    .addComponent(cbActions, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(91, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void bAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAddActionPerformed
    showCreateShift();
}//GEN-LAST:event_bAddActionPerformed

private void shiftTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_shiftTableMouseClicked
    setSelectedShift();
}//GEN-LAST:event_shiftTableMouseClicked

private void cbActionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbActionsActionPerformed
    switchAction();
}//GEN-LAST:event_cbActionsActionPerformed
    private RemedySchedulerFrame mainFarme;
    private ManagerDriver driver;
    private EditShiftTableModel model;
    private Shift selectedShift;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAdd;
    private javax.swing.JComboBox cbActions;
    private javax.swing.JTable shiftTable;
    private javax.swing.JScrollPane viewShift;
    // End of variables declaration//GEN-END:variables
}
